package com.xysd.express.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.xysd.express.R;
import com.xysd.express.adapter.CargoTypeAdpter;
import com.xysd.express.bean.CargoTypeObj;
import com.xysd.express.eventbus.TypeWeightEvent;
import com.xysd.express.util.DensityUtil;
import com.xysd.express.util.JSONParser;
import com.xysd.express.util.ToastUtil;
import com.xysd.express.util.http.HttpCallBack;
import com.xysd.express.util.http.HttpUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by donghui on 2017/12/11.
 */

public class MyDIYPopupWindow extends PopupWindow implements View.OnClickListener {
    Context context;
    private TextView cancle_tv,affirm_tv,curweight_tv,minweight_tv,maxwight_tv;
    private ImageView drag_btn;
    private RecyclerView goods_type_rv;
    private int dp240;//滑动容器的宽度；单位px
    private int curWeight=5;


    public MyDIYPopupWindow(Context context) {
        super(context);
        this.context = context;
        init();
    }

    private void init() {
        dp240=DensityUtil.dp2px(context,240);
        View contentView= LayoutInflater.from(context).inflate(R.layout.popup_diy,null);
        setContentView(contentView);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);// 设置弹出窗口的宽
        this.setHeight(dp240);// 设置弹出窗口的高
        this.setFocusable(true);// 设置弹出窗口可
        this.setAnimationStyle(R.style.pickerview_dialogAnim);// 设置动画
        this.setBackgroundDrawable(new ColorDrawable(0xffffffff));// 设置背景白色
        initView(contentView);
    }

    private void initView(View contentView) {
        cancle_tv= (TextView) contentView.findViewById(R.id.cancle_tv);
        affirm_tv= (TextView) contentView.findViewById(R.id.affirm_tv);
        curweight_tv= (TextView) contentView.findViewById(R.id.curweight_tv);
        minweight_tv= (TextView) contentView.findViewById(R.id.minweight_tv);
        maxwight_tv= (TextView) contentView.findViewById(R.id.maxwight_tv);
        drag_btn= (ImageView) contentView.findViewById(R.id.drag_btn);
        goods_type_rv= (RecyclerView) contentView.findViewById(R.id.goods_type_rv);
        cancle_tv.setOnClickListener(this);
        affirm_tv.setOnClickListener(this);

        setDragBtn();
        initRv();

    }

    boolean isTouch;
    boolean isFirst=true;
    private int sx,minx,maxx;
    private int sy;
    private void setDragBtn() {
        drag_btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (v.getId()==R.id.drag_btn){
                    switch (event.getAction()){
                        case MotionEvent.ACTION_DOWN:// 获取手指第一次接触屏幕
                            isTouch=true;
                            sx = (int) event.getRawX();
                            sy = (int) event.getRawY();
                            break;
                        case MotionEvent.ACTION_MOVE:
                            if (isTouch){
                                int x = (int) event.getRawX();
                                int y = (int) event.getRawY();
                                // 获取手指移动的距离
                                int dx = x - sx;
//                                int dy = y - sy;
                                int dy=0;//本自定义不变化Y轴
                                // 得到imageView最开始的各顶点的坐标
                                int l = drag_btn.getLeft();
                                int r = drag_btn.getRight();
                                int t = drag_btn.getTop();
                                int b = drag_btn.getBottom();
                                // 更改imageView在窗体的位置
                                if (l+dx<=0){//x轴小于0则置为容器x轴0的位置
                                    drag_btn.layout(0, t + dy, drag_btn.getWidth(), b + dy);
                                    curWeight=5;
                                    curweight_tv.setText("小于5公斤");
                                }
                                else if (l+dx>=dp240-drag_btn.getWidth()){//x轴大于容器宽度时置为x轴宽度的位置
                                    drag_btn.layout(dp240-drag_btn.getWidth(), t + dy, dp240, b + dy);
                                    curWeight=20;
                                    curweight_tv.setText("大于20公斤");
                                }else {//更改imageView在窗体的位置
                                    drag_btn.layout(l + dx, t + dy, r + dx, b + dy);
                                    curWeight=(l+dx)*15/(dp240-drag_btn.getWidth())+6;//起始位五公斤，而除法的第一个值为0，所以置第一个滑动重量为6；
                                    curweight_tv.setText(curWeight+"公斤");
                                }
                                Log.i("l,t,r,b","l + dx:"+(l + dx)+"\nt + dy:"+(t+dy)+"\nr + dx:"+(r + dx)+"\nb + dy:"+(b + dy));
                                // 获取移动后的位置
                                sx = (int) event.getRawX();
                                sy = (int) event.getRawY();
                                Log.i("sx--sy",sx+"--"+sy);
                            }
                            break;
                        case MotionEvent.ACTION_UP:
                            isTouch=false;
                            break;
                    }
                }
                return true;
            }
        });
    }


    public static CargoTypeAdpter adapter;
    List<CargoTypeObj.CargoType> datalist;

    int typeId;
    String cargoName,remark;
    private void initRv() {
        datalist=new ArrayList<>();
        adapter=new CargoTypeAdpter(R.layout.item_cargotype,context);
        goods_type_rv.setLayoutManager(new GridLayoutManager(context,3));
        goods_type_rv.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MyDIYPopupWindow.adapter.setCurSelect(position);
                typeId=datalist.get(position).Id;
            }
        });


        getData();

    }

    private void getData() {
        HttpUtil.getCargoType(new HttpCallBack() {
            @Override
            public void onSuccess(String result) {
                Log.i("物品类型",result);
                String code = JSONParser.getStringFromJsonString("Status",result);
                String msg = JSONParser.getStringFromJsonString("Msg",result);
                if ("0".equals(code)){
                    CargoTypeObj obj=new Gson().fromJson(result, CargoTypeObj.class);
                    datalist=obj.Data;
                    adapter.setNewData(datalist);
                }else {
                    ToastUtil.show(msg);
                }
            }

            @Override
            public void onFailure(String error) {
                Log.e("物品类型",error);
            }
        });
    }

    @Override
    public void setContentView(View contentView) {
        if(contentView != null) {
            contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
            super.setContentView(contentView);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.cancle_tv:
                dismiss();
                break;
            case R.id.affirm_tv:
                EventBus.getDefault().post(new TypeWeightEvent(typeId,curWeight));
                dismiss();
                break;
        }
    }
}
